﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Free2AskMVC4.Models;
namespace Free2AskMVC4.Helpers
{
    public static class Extensions
    {
        public static MvcHtmlString RichTextareaFor(this HtmlHelper helper, string id, string text = "")
        {
            //return new MvcHtmlString(String.Format(@"<textarea id="{0}" class="tinymce">{1}</textarea>",id,text));
            return new MvcHtmlString(String.Format(@"<textarea id=""{0}"" class=""tinymce"">{1}</textarea>",id,text));
        }

        public static string StripHtmlTags(this string post)
        {
            if (post == null)
            {
                return "";
            }
            else
            {
                return Regex.Replace(post, @"<[^>]*>", String.Empty);
            }
        }

        public static string Truncate(this string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }
            else
            {
                return input.Substring(0, length) + "...";
            }
        }
    }

    public static class PagingHelpers
    {
        ///  <summary>
        /// This method returns an MVCHtmlString which
        /// is used for Paging purpose. this represents
        /// that it is an extension method, PageInfo is a
        /// ViewModel class which I had created and pageUrl
        /// is anomyous type function.
        ///  </summary>
        public static MvcHtmlString PageLinks(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            //Create an object of StringBuilder class
            StringBuilder result = new StringBuilder();

            for (int i = 1; i <= pageInfo.TotalPages; i++)
            {
                //TagBuilder class is used to create Tag.
                //Here I am creating <a></a> tag.
                TagBuilder tag = new TagBuilder("a");
                //Merge required attributed like href, title etc.
                tag.MergeAttribute("href", pageUrl(i));
                //Set inner heml.
                tag.InnerHtml = i.ToString();

                if (i == pageInfo.CurrentPage)
                {
                    //Add any class if you want to add.
                    tag.AddCssClass("selected");
                }
                //Finally add resulted string in StringBuilder object.
                result.Append(tag.ToString());
            }

            //Create MVCHtmlString.
            return MvcHtmlString.Create(result.ToString());
        }
    }
}